package com.dawn.login.action;

import java.io.ByteArrayInputStream;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import weibo4j.http.AccessToken;
import weibo4j.http.RequestToken;

import com.dawn.login.authentication.SinaOAuth;
import com.dawn.utils.PropertiesTools;
import com.opensymphony.xwork2.ActionSupport;

@Controller("sinaAuthAction")
@Scope("prototype")
public class SinaAuthAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = -4625459221032797612L;

	private String call_back_url;

	@Autowired
	private SinaOAuth sinaOAuth;
	private Map<String, Object> session;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private InputStream inputStream;

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getURL() throws IOException {
		call_back_url = PropertiesTools.getInstance().getProperty(
				"call_back_url");
		sinaOAuth = new SinaOAuth();
		RequestToken requestToken = sinaOAuth.getRequestToken(call_back_url);
		if (requestToken != null) {
			session.put("resToken", requestToken);
			String url = requestToken.getAuthorizationURL()
					+ "&oauth_callback=" + call_back_url;
			this.inputStream = new ByteArrayInputStream(url.getBytes());
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String getAccessToken() throws IOException {
		call_back_url = PropertiesTools.getInstance().getProperty(
				"call_back_url");
		System.out.println(PropertiesTools.getInstance().getProperty("call_back_url"));
		sinaOAuth = new SinaOAuth();
		RequestToken requestToken = sinaOAuth.getRequestToken(call_back_url);
		if (requestToken != null) {
			session.put("resToken", requestToken);
			String url = requestToken.getAuthorizationURL()
					+ "&oauth_callback=" + call_back_url;
			
			System.out.println(url);
			response.sendRedirect(url);
			return null;
		} else {
			return ERROR;
		}
	}

	public String getOAuth() {
		String verifier = (String) request.getParameter("oauth_verifier");
		if (verifier != null) {
			System.out.println("oauth:" + verifier);
			RequestToken resToken = (RequestToken) session.get("resToken");
			if (resToken != null) {
				AccessToken accessToken = sinaOAuth.gettAccessToken(resToken,
						verifier);
				if (accessToken != null) {
					session.put("accToken", accessToken.getToken());
					session.put("accTokenSecret", accessToken.getTokenSecret());
					session.put("sinaId", accessToken.getUserId());
					return SUCCESS;
				} else {
					System.out.println("access token request error");
				}
			} else {
				System.out.println("request token session error");
			}
		} else {
			System.out.println("verifier String error");
		}
		return ERROR;
	}

}
